from datetime import datetime
from typing import Optional, List, Any
from pydantic import BaseModel, Field, validator, ConfigDict

from enum import Enum


class DimensionNameEnum(str, Enum):
    """
    评分维度名称枚举（保留用于向后兼容）
    定义5个核心评分维度
    """
    CONCEPT = "作品概念"
    ART = "画面美术"
    ANIMATION = "动画表演"
    AUDIOVISUAL = "视听语言"
    INNOVATION = "创新性"


class ScoreDimensionBase(BaseModel):
    """
    评分维度基础模型
    包含评分维度的基本信息字段
    """
    dimension_name: str = Field(..., description="维度名称（支持自定义维度）", max_length=50)
    dimension_weight: int = Field(..., description="维度权重（整数，如30表示30%）", ge=0, le=100)
    dimension_desc: str = Field(..., description="维度核心描述", max_length=500)
    
    @validator('dimension_name')
    def validate_dimension_name(cls, v):
        """
        验证维度名称
        确保维度名称不为空且长度合适
        """
        if not v or not v.strip():
            raise ValueError('维度名称不能为空')
        if len(v.strip()) > 50:
            raise ValueError('维度名称长度不能超过50个字符')
        return v.strip()


class ScoreDimensionCreate(ScoreDimensionBase):
    """
    创建评分维度模型
    用于创建新维度时的数据验证
    """
    pass


class ScoreDimensionUpdate(BaseModel):
    """
    更新评分维度模型
    用于更新维度信息时的数据验证
    """
    dimension_name: Optional[str] = Field(None, description="维度名称", max_length=50)
    dimension_weight: Optional[int] = Field(None, description="维度权重", ge=0, le=100)
    dimension_desc: Optional[str] = Field(None, description="维度核心描述", max_length=500)
    
    @validator('dimension_name')
    def validate_dimension_name(cls, v):
        """
        验证维度名称
        确保维度名称不为空且长度合适
        """
        if v is not None:
            if not v or not v.strip():
                raise ValueError('维度名称不能为空')
            if len(v.strip()) > 50:
                raise ValueError('维度名称长度不能超过50个字符')
            return v.strip()
        return v


class ScoreDimensionResponse(ScoreDimensionBase):
    """
    评分维度响应模型
    用于返回维度信息
    """
    dimension_id: int = Field(..., description="维度唯一ID")
    create_time: datetime = Field(..., description="配置创建时间")

    model_config = ConfigDict(from_attributes=True)


class ScoreDimensionListResponse(BaseModel):
    """
    评分维度列表响应模型
    用于返回维度列表信息
    """
    total: int = Field(..., description="总数量")
    items: List[ScoreDimensionResponse] = Field(..., description="维度列表")


class DimensionQuery(BaseModel):
    """
    评分维度查询模型
    用于分页查询和搜索
    """
    page: int = Field(1, description="页码", ge=1)
    size: int = Field(10, description="每页数量", ge=1, le=100)
    keyword: Optional[str] = Field(None, description="搜索关键词")
    dimension_name: Optional[str] = Field(None, description="按维度名称筛选", max_length=50)


class DimensionWeightUpdate(BaseModel):
    """
    批量更新维度权重模型
    用于一次性更新所有维度的权重配置
    """
    weights: List[dict] = Field(..., description="权重配置列表，格式：[{\"dimension_name\": \"作品概念\", \"weight\": 30}]")

    model_config = ConfigDict(json_schema_extra={
        "example": {
            "weights": [
                {"dimension_name": "作品概念", "weight": 30},
                {"dimension_name": "画面美术", "weight": 25},
                {"dimension_name": "动画表演", "weight": 20},
                {"dimension_name": "视听语言", "weight": 15},
                {"dimension_name": "创新性", "weight": 10}
            ]
        }
    })


# ==================== 统一API响应模型 ====================

class ApiResponse(BaseModel):
    """
    统一API响应模型
    所有接口都应使用此格式返回数据
    """
    code: int = Field(..., description="响应状态码，200表示成功")
    msg: str = Field(..., description="响应消息")
    data: Optional[Any] = Field(None, description="响应数据，可选")

    model_config = ConfigDict(json_schema_extra={
        "example": {
            "code": 200,
            "msg": "操作成功",
            "data": {}
        }
    })


class ApiResponseWithData(BaseModel):
    """
    带数据的API响应模型
    用于需要返回具体数据的接口
    """
    code: int = Field(..., description="响应状态码，200表示成功")
    msg: str = Field(..., description="响应消息")
    data: Any = Field(..., description="响应数据")

    model_config = ConfigDict(json_schema_extra={
        "example": {
            "code": 200,
            "msg": "获取数据成功",
            "data": {}
        }
    })


class ApiResponseNoData(BaseModel):
    """
    无数据的API响应模型
    用于不需要返回具体数据的接口（如删除、更新操作）
    """
    code: int = Field(..., description="响应状态码，200表示成功")
    msg: str = Field(..., description="响应消息")

    model_config = ConfigDict(json_schema_extra={
        "example": {
            "code": 200,
            "msg": "操作成功"
        }
    })